FROM node:22-alpine AS base
#FROM node:22-bullseye-slim as base
RUN apk update && apk add --no-cache libc6-compat
RUN npm install pnpm turbo --global
RUN pnpm config set store-dir ~/.pnpm-store

FROM base AS builder
WORKDIR /app
COPY . .
RUN turbo prune web --docker

FROM base AS installer
WORKDIR /app
ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL
ARG BACK_SERVER_URL
ENV BACK_SERVER_URL=$BACK_SERVER_URL
RUN echo "Building web, BACK_SERVER_URL:$BACK_SERVER_URL DATABASE_URL:$DATABASE_URL"

COPY --from=builder /app/out/json/ .
RUN pnpm install
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json
RUN turbo run build --filter web

FROM base AS web
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone/ ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
EXPOSE 3000
ENV PORT=3000
USER nextjs

CMD ["node", "apps/web/server.js"]
